package com.tide.crm.controller;

import java.util.Date;
import java.util.List;

import com.tide.crm.comm.BaseController;
import com.tide.crm.dto.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tide.crm.page.table.PageTableRequest;
import com.tide.crm.page.table.PageTableHandler;
import com.tide.crm.page.table.PageTableResponse;
import com.tide.crm.page.table.PageTableHandler.CountHandler;
import com.tide.crm.page.table.PageTableHandler.ListHandler;
import com.tide.crm.dao.CustomerEventDao;
import com.tide.crm.model.CustomerEvent;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;

@RestController
@RequestMapping("/customerEvents")
public class CustomerEventController extends BaseController {

    @Autowired
    private CustomerEventDao customerEventDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public CustomerEvent save(@RequestBody CustomerEvent customerEvent, HttpServletRequest request) {
        Date date = new Date();
        LoginUser user = super.getUserInfoByToken(request);
        customerEvent.setCreateDate(date);
        customerEvent.setModifyDate(date);
        customerEvent.setCreater(user.getUsername());
        customerEvent.setModifyer(user.getUsername());
        customerEventDao.save(customerEvent);

        return customerEvent;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public CustomerEvent get(@PathVariable Long id) {
        return customerEventDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public CustomerEvent update(@RequestBody CustomerEvent customerEvent) {
        customerEventDao.update(customerEvent);

        return customerEvent;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return customerEventDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<CustomerEvent> list(PageTableRequest request) {
                return customerEventDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        customerEventDao.delete(id);
    }
}
